//
//  QSTabBtnView.swift
//  TestSwift
//
//  Created by Song on 2020/5/9.
//  Copyright © 2020 Song. All rights reserved.
//

import QSBaseView
import RxSwift
import RxCocoa

class QSTabBtnView: QSBaseView {
    override func qs_setupSubViews() {
        self.addSubview(titleLab)
        titleLab.snp.makeConstraints { (make) in
            make.centerX.centerY.equalToSuperview()
            make.left.greaterThanOrEqualToSuperview()
            make.right.lessThanOrEqualToSuperview()
        }
        
        self.addSubview(btn)
        btn.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    override func qs_binding() {
        btn.rx.tap.asObservable()
            .bind { [unowned self] _ in
                self.qs_delegate?.qs_clickTitleBtnView(btnView: self)
        }.disposed(by: disposeBag)
    }
    
    // MARK: - Property
    private let disposeBag = DisposeBag()
    private weak var qs_delegate: QSTabTitleBtnViewDelegate?
    
    // MARK: - Widget
    private lazy var titleLab: UILabel = {
        let lab = UILabel.init()
            .qs_textAlignment(.center)
        return lab
    }()
    
    private lazy var btn: UIButton = {
        let b = UIButton.init()
        return b
    }()
}

extension QSTabBtnView: QSTabTitleBtnViewProtocol {
    func qs_setDelegate<T>(_ delegate: T) where T : QSTabTitleBtnViewDelegate {
        qs_delegate = delegate
    }
    
    func qs_setTitle(_ title: String) {
        titleLab.text = title
    }
    
    func qs_setTitleFont(_ font: UIFont) {
        titleLab.font = font
    }
    
    func qs_setTitleColor(_ color: UIColor) {
        titleLab.textColor = color
    }
    
    func qs_isSelected(_ isSelected: Bool) {
        btn.isEnabled = !isSelected
    }
}
